package net.xju_5_6.mapper;

import net.xju_5_6.entity.Student;
import net.xju_5_6.entity.StudentCard;
import net.xju_5_6.entity.Vo.ClazzTeachVo;
import net.xju_5_6.entity.Vo.ClazzVO;
import net.xju_5_6.entity.Vo.StuCardVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

//@CacheNamespace  // 注解 开启二级缓存
public interface StudentMapper {

//    @Select("select * from  student")
    public List<Student> selectList();
    public Integer insert(Student student);
    public Integer update(Student student);
    public Integer delete(Integer id);

    //@Param:指定参数名称
    @Select(" select * from  student where name =#{name} and  sn =#{sn}")
    Student selectBySnAndName(@Param("name") String name,@Param("sn") String sn);

    Student selectByMap(Map<String, Object> map);

    @MapKey("name")
    Map<String, Object> selectMap(String name);

//    @Select("SELECT s.*,c.* FROM `student` s,card c WHERE s.card_id = c.id AND s.id= #{id}")
//    @Results({
//            @Result(property = "id",column = "id",id = true),
//            @Result(property = "sn",column = "sn"),
//            @Result(property = "name",column = "name"),
//            @Result(property = "clazzId",column = "clazz_id"),
//            @Result(property = "cardId",column = "card_id"),
//            @Result(property = "studentCard.id",column = "id"),
//            @Result(property = "studentCard.balance",column = "balance"),
//    })

    @Select("select  * from student where id = #{id}")
            @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "sn",column = "sn"),
            @Result(property = "name",column = "name"),
            @Result(property = "clazzId",column = "clazz_id"),
            @Result(property = "cardId",column = "card_id"),
            @Result(property = "studentCard",
            column = "card_id",
            javaType = StudentCard.class,
            one = @One(select = "net.xju_5_6.mapper.StudentMapper.selectByCardId")),

    })
    StuCardVO selectById(int id);
    @Select("select * from card where id = #{id}")
    StudentCard selectByCardId(Integer id);

    @Select("select * from  clazz where id =#{id}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "clazzName",column = "clazz_name"),
            @Result(property = "sum",column = "sum"),
            @Result(property = "studentList",
            column = "id",
            javaType = List.class,
            many = @Many(select = "net.xju_5_6.mapper.StudentMapper.selectByClazzId"))
    })
    ClazzVO selectClazzVo(int id);
    @Select("select * from student where clazz_id = #{id}")
    List<Student> selectByClazzId(int id);

    List<ClazzTeachVo> selectClazzTeachVo();

//    @Select("<script>" +
//            "select  * from   student where" +
//            "            <choose>" +
//            "                <when test='sn !='' and sn !=null'>" +
//            "                    sn =#{sn}" +
//            "                </when>" +
//            "                <when test='name !='' and name !=null'>" +
//            "                    name =#{name}" +
//            "                </when>" +
//            "                <otherwise>1=1</otherwise>" +
//            "            </choose>" +
//            "</script>")
    List<Student> selectStudents(Student student);

    @Select("<script>" +
            "select * from student WHERE id IN" +
            "        <foreach collection='ids' open='(' item='id' separator=',' close=')'>" +
            "            #{id}" +
            "        </foreach>" +
            "</script>")
    List<Student> selectByIds(@Param("ids") List<Integer> ids);

    Integer batchInsert(@Param("students") List<Student> students);
}
